package cn.newgrand.netcall.consultant;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.swing.JOptionPane;

import org.jivesoftware.smack.PacketCollector;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketIDFilter;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.IQ.Type;
import org.jivesoftware.spark.PluginManager;
import org.jivesoftware.spark.SparkManager;

import cn.newgrand.netcall.base.ContactBean;
import cn.newgrand.netcall.consultant.ui.SpecialistGroup;
import cn.newgrand.netcall.consultant.ui.SpecialistItem;
import cn.newgrand.netcall.consultant.ui.SpecialistList;
import cn.newgrand.smackx.consultant.entity.SearchSpecialistEntity;
import cn.newgrand.smackx.consultant.packet.SpecialistQueueSize;
import cn.newgrand.smackx.consultant.packet.SpecialistSearch;

public class SearchManager {

	//private SpecialistRoster roster;
	
//	public SearchManager(SpecialistRoster roster){
//		this.roster =roster;
//	}
	public SearchManager(){
	}
	//spid username field currentsize/maxsize(send a packet) isonline(用于判断是否应该显示在搜索结果里)
	public List<SearchSpecialistEntity> searchSpecialistEntity(SeachType type,String field1,String field2,String field3){
		List<SearchSpecialistEntity> list = new ArrayList<SearchSpecialistEntity>();
		SpecialistList p = ((ConsultantPlugin)PluginManager.getInstance().getPlugin(ConsultantPlugin.class)).getSpecialistList();
		SpecialistQueueSize queues = new SpecialistQueueSize();
		queues.setType(Type.GET);
		queues.setTo("newgrandAgentExpert."+SparkManager.getConnection().getServiceName());
		PacketFilter filter = new PacketIDFilter(queues.getPacketID());
		PacketCollector collector = SparkManager.getConnection().createPacketCollector(filter);
		
		switch (type){
		case detail://field=spid=SpecialistRosterEntry.name field2=username=SpecialistRosterEntry.user
			//SearchSpecialistEntity entity = new SearchSpecialistEntity(roster.getEntries().)
			if(field1!=null&&!field1.isEmpty()){
				//SpecialistItem item = p.getContactItemByDisplayName(field1);
				SpecialistItem item = p.getContactItemBySpid(field1,true);
				if(item!=null ){//&& item.isAvailable()){
					SearchSpecialistEntity entity = new SearchSpecialistEntity(item.getAlias(), item.getJID(), item.getGroupName(), item.getNickname(), item.isAvailable());
					queues.addItem(item.getAlias(), "");
					list.add(entity);
				}
			}else if(field2!=null&&!field2.isEmpty()){
				Map<String,ContactBean> items = p.getAllContacts();
				for(ContactBean bean:items.values()){
					if(bean.getNickName().equals(field2)){
						//if(SpecialistPresenceManager.getSpecialistPresence(bean.getName()).isAvailable()){
							SearchSpecialistEntity entity = new SearchSpecialistEntity(bean.getName(), bean.getJID(), bean.getGroupName(), bean.getNickName(),SpecialistPresenceManager.getSpecialistPresence(bean.getName()).isAvailable());
							queues.addItem(bean.getName(), "");
							list.add(entity);
						//}
					}
				}
			}else if((field2==null||field2.isEmpty())&&(field1==null||field1.isEmpty())){
				Map<String,ContactBean> items = p.getAllContacts();
				for(ContactBean bean:items.values()){
					//if(SpecialistPresenceManager.getSpecialistPresence(bean.getName()).isAvailable()){
						SearchSpecialistEntity entity = new SearchSpecialistEntity(bean.getName(), bean.getJID(), bean.getGroupName(), bean.getNickName(),SpecialistPresenceManager.getSpecialistPresence(bean.getName()).isAvailable());
						queues.addItem(bean.getName(), "");
						list.add(entity);
					//}
				}
			}
			break;
		case field:
			if(field1!=null&&field2==null){
				for(SpecialistItem item:p.getContactGroup("领域").getContactGroup("领域::"+field1).getContactItems(true)){
					if(item!=null){// && item.isAvailable()){
						SearchSpecialistEntity entity = new SearchSpecialistEntity(item.getAlias(), item.getJID(), item.getGroupName(), item.getNickname(),item.isAvailable());
						queues.addItem(item.getAlias(), "");
						list.add(entity);
					}
				}
			}else if(field1!=null&&field2!=null){
				for(SpecialistItem item:p.getContactGroup("领域").getContactGroup("领域::"+field1).getContactGroup("领域::"+field1+"::"+field2).getContactItems(true)){
					if(item!=null){// && item.isAvailable()){
						SearchSpecialistEntity entity = new SearchSpecialistEntity(item.getAlias(), item.getJID(), item.getGroupName(), item.getNickname(),item.isAvailable());
						queues.addItem(item.getAlias(), "");
						list.add(entity);
					}
				}
			}
			break;
		case product://产品 套件 模块
			if(field1!=null&&field3==null){
				for(SpecialistItem item:p.getContactGroup("产品").getContactGroup("产品::"+field1).getContactItems(true)){
					if(item!=null ){//&& item.isAvailable()){
						SearchSpecialistEntity entity = new SearchSpecialistEntity(item.getAlias(), item.getJID(), item.getGroupName(), item.getNickname(),item.isAvailable());
						queues.addItem(item.getAlias(), "");
						list.add(entity);
					}
				}
			}else if(field1!=null&&field3!=null&&field2==null){
				for(SpecialistItem item:p.getContactGroup("产品").getContactGroup("产品::"+field1).getContactGroup("产品::"+field1+"::"+field3).getContactItems(true)){
					if(item!=null ){// && item.isAvailable()){
						SearchSpecialistEntity entity = new SearchSpecialistEntity(item.getAlias(), item.getJID(), item.getGroupName(),  item.getNickname(),item.isAvailable());
						queues.addItem(item.getAlias(), "");
						list.add(entity);
					}
				}
			}else if(field1!=null&&field3!=null&&field2!=null){
				for(SpecialistItem item:p.getContactGroup("产品").getContactGroup("产品::"+field1).getContactGroup("产品::"+field1+"::"+field3).getContactGroup("产品::"+field1+"::"+field3+"::"+field2).getContactItems(true)){
					if(item!=null ){// && item.isAvailable()){
						SearchSpecialistEntity entity = new SearchSpecialistEntity(item.getAlias(), item.getJID(), item.getGroupName(),  item.getNickname(),item.isAvailable());
						queues.addItem(item.getAlias(), "");
						list.add(entity);
					}
				}
			}
//			for(SpecialistItem item:p.getContactGroup("离线的联系人").getContactItems()){
//				SearchSpecialistEntity entity = new SearchSpecialistEntity(item.getAlias(), item.getJID(), item.getGroupName());
//				queues.addItem(item.getAlias(), "");
//				list.add(entity);
//			}
			break;
		case trade:
			if(field1!=null){
				for(SpecialistItem item:p.getContactGroup("行业").getContactGroup("行业::"+field1).getContactItems(true)){
					if(item!=null  ){//&& item.isAvailable()){
						SearchSpecialistEntity entity = new SearchSpecialistEntity(item.getAlias(), item.getJID(), item.getGroupName(), item.getNickname(), item.isAvailable());
						queues.addItem(item.getAlias(), "");
						list.add(entity);
					}
				}
			}
			break;
		default:
			break;
		}
		if(queues.isEmpty()){
			return list;
		}
		SparkManager.getConnection().sendPacket(queues);
		IQ packet = (IQ)collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
		if(packet.getType().equals(IQ.Type.ERROR)){
			JOptionPane.showMessageDialog(null, "获取专家队列状态失败");
			return list;
		}
		SpecialistQueueSize result = (SpecialistQueueSize)packet;
		for(SearchSpecialistEntity e:list){
			e.setSerivceQueue(result.getSizeText(e.getSpid()));
		}
		return list;
	}
	
	public Map<String,SpecialistSearch.SpecialistSearchField> getDefaultLabel(){
		SpecialistSearch iqSearch = new SpecialistSearch();
		iqSearch.setFrom(SparkManager.getConnection().getUser());
		iqSearch.setTo("newgrandAgent."+SparkManager.getConnection().getServiceName());
		iqSearch.setType(Type.GET);
		PacketFilter filter = new PacketIDFilter(iqSearch.getPacketID());
		PacketCollector collector = SparkManager.getConnection().createPacketCollector(filter);
		SparkManager.getConnection().sendPacket(iqSearch);
		SpecialistSearch result = (SpecialistSearch)collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
		return result.getSupportField();
//		SpecialistSearch search = new SpecialistSearch();
//		SpecialistSearchField field = search.new SpecialistSearchField("产品+模块+套件");
//		field.addLabelDefault("产品", "I6");
//		field.addLabelDefault("产品", "A3");
//		field.addLabelDefault("套件", "柔性统一平台");
//		field.addLabelDefault("模块", "业务建模");
//		search.addFieldList(field);
//		Map<String,SpecialistSearch.SpecialistSearchField> map = search.getSupportField();
//
//		return map;
	}
//	public Map<String,SpecialistSearch.SpecialistSearchField> getDefaultLabel(boolean isLocal){
//		SpecialistList p = ((ConsultantPlugin)PluginManager.getInstance().getPlugin(ConsultantPlugin.class)).getSpecialistList();
//		SpecialistGroup g = p.getActiveGroup();
//		
//		SpecialistSearch search = new SpecialistSearch();
//		SpecialistSearchField field = search.new SpecialistSearchField("产品+模块+套件");
//		SpecialistSearchField field2 = search.new SpecialistSearchField("行业");
//		SpecialistSearchField field3 = search.new SpecialistSearchField("领域");
//		
//		for(SpecialistGroup gg:g.getContactGroups()){
//			if(gg.getGroupTitle(gg.getGroupName()).equals("产品")){
//				
//				field
//			}
//			if(gg.getGroupTitle(gg.getGroupName()).equals("行业")){
//				field2
//			}
//			if(gg.getGroupTitle(gg.getGroupName()).equals("领域")){
//				field3
//			}
//		}
//		search.addFieldList(field);
//		search.addFieldList(field2);
//		search.addFieldList(field3);
//		Map<String,SpecialistSearch.SpecialistSearchField> map = search.getSupportField();
//
//		return map;
//	}
	
	public List<String> getItems(String menuName){
		SpecialistList p = ((ConsultantPlugin)PluginManager.getInstance().getPlugin(ConsultantPlugin.class)).getSpecialistList();
		 SpecialistGroup g = p.getContactGroup(menuName);

		//SpecialistGroup gg = getGroupName(g,menuName);
		List<String> strs = new ArrayList<String>();
		if(g!=null){
			for(SpecialistGroup ggg:g.getContactGroups()){
				strs.add(ggg.getGroupTitle(ggg.getGroupName()));
			}
		}
		return strs;
	}
	
	public SpecialistGroup getGroupName(SpecialistGroup g,String name){
		for(SpecialistGroup gg:g.getContactGroups()){
			if(gg.getGroupTitle(gg.getGroupName()).equals(name)){
				return gg;
			}else{
				SpecialistGroup ggg = getGroupName(gg,name);
				if(ggg!=null){
					return ggg;
				}
			}
		}
		return null;
	}
	
    public enum SeachType {

        detail,

        field,

        product,

        trade,
        
        none;
    }
}
